package com.webproject.shop.service.impl;

import com.webproject.shop.common.BusinessException;
import com.webproject.shop.common.ErrorCode;
import com.webproject.shop.dao.AdminMapper;
import com.webproject.shop.dao.ProductMapper;
import com.webproject.shop.pojo.Admin;
import com.webproject.shop.pojo.Product;
import com.webproject.shop.pojo.User;
import com.webproject.shop.service.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService , ErrorCode {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private ProductMapper productMapper;

    //登录模块
    @Override
    public Admin login(String username, String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new BusinessException(PARAMETER_ERROR, "参数不合法！");
        }
        Admin admin = adminMapper.selectByPrimaryKey(username);
        if (admin == null || !StringUtils.equals(password, admin.getPassword())) {
            throw new BusinessException(USER_LOGIN_FAILURE, "账号或密码错误！");
        }
        return admin;
    }

    //增加商品
    @Override
    public void addProduct(Product product){
        productMapper.insert(product);
    }

    //删除商品
    @Override
    public void deleteProduct(int id){
        productMapper.deleteByPrimaryKey(id);
    }

    //更新商品
    @Override
    public void updateProduct(Product product){
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public List<Product> findAll() {
        //全部商品列表
        List<Product> products = productMapper.selectAll();
        return products;
    }
}
